package com.theme.passport.controller.sys;

import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.SysMessageEntity;
import com.theme.passport.entity.SysUserEntity;
import com.theme.passport.entity.constant.MessageTypeEnum;
import com.theme.passport.service.SysMessageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


/**
 * 消息表
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2020-06-12 17:45:41
 */
@RestController
@RequestMapping("sys/message")
public class SysMessageController extends AbstractController{
    @Autowired
    private SysMessageService sysMessageService;

    @RequestMapping("/send")
    @RequiresPermissions("sys:message:save")
    public R send(@RequestBody SysMessageEntity sysMessage) {
        // 获取当前用户
        SysUserEntity user = getUser();
        // 组装数据
        sysMessage.setFromUserId(user.getUserId());
        sysMessage.setFromNickName(user.getNickname());
        sysMessage.setCreateTime(new Date());
        sysMessage.setNotifyCode(System.currentTimeMillis()+"");
        if (sysMessage.getType().equals(MessageTypeEnum.SYSTEM_NOTICE.getValue())){
            sysMessageService.save(sysMessage);
            return R.ok();
        }
        return sysMessageService.sendMessage(sysMessage);
    }

    @RequestMapping("/sendNotice")
    @RequiresPermissions("sys:message:save")
    public R sendNotice(@RequestBody SysMessageEntity sysMessage) {
        // 获取当前用户
        SysUserEntity user = getUser();
        // 组装数据
        sysMessage.setFromUserId(user.getUserId());
        sysMessage.setFromNickName(user.getNickname());
        sysMessage.setCreateTime(new Date());
        sysMessage.setNotifyCode(System.currentTimeMillis()+"");
        if (sysMessage.getType().equals(MessageTypeEnum.SYSTEM_NOTICE.getValue())){
            sysMessageService.save(sysMessage);
            return R.ok();
        }

        sysMessageService.save(sysMessage);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:message:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysMessageService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:message:info")
    public R info(@PathVariable("id") Long id) {
        SysMessageEntity sysMessage = sysMessageService.getById(id);

        return R.ok().put("sysMessage", sysMessage);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:message:save")
    public R save(@RequestBody SysMessageEntity sysMessage) {
        sysMessageService.save(sysMessage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:message:update")
    public R update(@RequestBody SysMessageEntity sysMessage) {
        sysMessageService.updateById(sysMessage);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:message:delete")
    public R delete(@RequestBody Long[] ids) {
        sysMessageService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
